﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using OpenLab.Reports;
using OpenLab.UI.Helper;
using OpenLab.UI.Parametrage;
using OpenLab.UI.Shared;

namespace OpenLab.UI.Main
{
    public partial class MainMenu : Form
    {
        public MainMenu()
        {
            InitializeComponent();
            Load += MainMenu_Load;
            Utile.LoadAllClient();
        }

        void MainMenu_Load(object sender, EventArgs e)
        {
            new Login().ShowDialog();
            if (!StaticVar.LogedIn) Close();
            else ribbon1.Visible = true;
        }

        void OpenForm(string formName)
        {
            if (!formName.StartsWith("OpenLab.UI.")) formName = "OpenLab.UI." + formName;

            Type type = Type.GetType(formName);
            Debug.Assert(type != null, "type != null");
            dynamic f = Activator.CreateInstance(type);
            // ReSharper disable once NotResolvedInText
            if (f == null) throw new ArgumentNullException("Form null");

            foreach (Form frm in MdiChildren)
            {
                if (frm.GetType() == type)
                {
                    frm.Activate();
                    return;
                }
            }
            f.MdiParent = this;
            f.Show();
            var form = f as Form;
            if (form != null) form.Activated += LoadForm;
        }
        void LoadForm(object sender, EventArgs e)
        {
            if (((Form) sender).WindowState == FormWindowState.Maximized) return;
            ((Form) sender).WindowState = FormWindowState.Maximized;
            ((Form) sender).ControlBox = false;
            ((Form) sender).BringToFront();
        }

        private void ajoutdemande_Click(object sender, EventArgs e)
        {
            OpenForm("Reception.AjoutDemande");
        }

        private void listedemande_Click(object sender, EventArgs e)
        {
            OpenForm("Reception.ListeDemande");
        }

        private void supprimerdemande_Click(object sender, EventArgs e)
        {
            OpenForm("Reception.SupprimeDemande");
        }

        private void resutat_Click(object sender, EventArgs e)
        {
            OpenForm("Resultat.Resultat");
        }

        private void validation_Click(object sender, EventArgs e)
        {
            OpenForm("Resultat.Validation");
        }

        private void organismebtn_Click(object sender, EventArgs e)
        {
            OpenForm("Parametrage.Organisme");
        }

        private void pricebtn_Click(object sender, EventArgs e)
        {
            OpenForm("Parametrage.PrixAnalyse");
        }

        private void generalebtn_Click(object sender, EventArgs e)
        {
            OpenForm("Parametrage.Generale");
        }

        private void analysebtn_Click(object sender, EventArgs e)
        {
            OpenForm("Parametrage.Analyse");
        }

        private void antibiotiquebtn_Click(object sender, EventArgs e)
        {
            OpenForm("Parametrage.Antibiotique");
        }

        private void germebtn_Click(object sender, EventArgs e)
        {
            new Germe().ShowDialog();
        }

        private void rapportfinancier_Click(object sender, EventArgs e)
        {
            new Financier().ShowDialog();
        }
    }
}
